'''
This file is part of ModeliMark.

ModeliMark is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

ModeliMark is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with ModeliMark.
If not, see <http://www.gnu.org/licenses/>.

Copyright 2012 Jens Frenkel
'''

import os
from modelimark.wrapper import BaseWrapper
from modelimark.modelgenerator.basemodelgenerator import BaseModelGenerator

class BenchmarkComponent(object):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''


    def runBenchmark(self, compiler, mgenerator, workdir, resfilename, range ):
        '''

        '''

        assert(isinstance(compiler,BaseWrapper))
        assert(isinstance(mgenerator,BaseModelGenerator))

        # new directory
        if not os.access(workdir, os.F_OK):
            os.mkdir(workdir)
        # enter
        oldpath = os.getcwd()
        os.chdir(workdir)

        f = open( resfilename, 'w' )
        f.write('"N";"open";"flatten";"translate";"simulate"\n')
        for n in range:
            id = mgenerator.modelIdentifier(n)
            print 'Model: %s'%str(id)
            if (isinstance(id,(float,int))):
                f.write('%s;'%str(id))
            else:
                f.write('"%s";'%str(id))
            # generate model source
            model,files,modeldir,libs = mgenerator.generateModelSource(n,workdir)
            # enter model source directory
            os.chdir(modeldir)
            # benchmark
            resopen, dtopen = compiler.open(model,files,libs)
            if (resopen is not False):
                f.write('%s;'%str(dtopen))
            else:
                f.write('NONE;')
            resflatten, dtflatten = compiler.flatten(model,files,libs)
            if (resflatten is not False):
                f.write('%s;'%str(dtflatten))
            else:
                f.write('NONE;')
            restranslate, dttranslate = compiler.translate(model,files,libs)
            if (restranslate is not False):
                f.write('%s;'%str(dttranslate))
            else:
                f.write('NONE;')
            ressimulate, dtsimulate = compiler.simulate(model,files,libs,0,0.1,1)
            if (ressimulate is not False):
                f.write('%s;'%str(dtsimulate))
            else:
                f.write('NONE;')
            f.write('\n')
        f.close()

        os.chdir(oldpath)
